package com.zxjbyte.yiyi.module.plugin.job.controller;

import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.module.plugin.job.domain.request.QJobAddRequest;
import com.zxjbyte.yiyi.module.plugin.job.domain.request.QJobPageRequest;
import com.zxjbyte.yiyi.module.plugin.job.domain.request.QJobUpdateRequest;
import com.zxjbyte.yiyi.module.plugin.job.domain.response.QJobPageResponse;
import com.zxjbyte.yiyi.module.plugin.job.service.IQuartzJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 基于quartz实现在线定时任务管理
 * 源码地址：https://github.com/quartz-scheduler/quartz
 * @Author zhangxingjia
 * @Date 2023/8/23 17:31
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "定时任务在线管理")
@RestController
@RequestMapping(value = Constant.BASE_PATH_PLUGIN + "/qjob")
public class QuartzJobController {

    @Resource
    private IQuartzJobService quartzJobService;

    @Operation(summary = "添加定时任务")
    @PostMapping("/add")
    public R<?> addJob(@Valid @RequestBody QJobAddRequest addRequest){
        quartzJobService.add(addRequest);
        return R.ok();
    }

    @Operation(summary = "定时任务分页查询")
    @GetMapping("/page")
    public PageResult<QJobPageResponse> page(@ParameterObject QJobPageRequest pageRequest){
        return quartzJobService.page(pageRequest);
    }

    @Operation(summary = "更新定时任务")
    @PutMapping("/update")
    public R<?> updateJob(@Valid @RequestBody QJobUpdateRequest updateRequest){
        quartzJobService.updateJob(updateRequest);
        return R.ok();
    }

    @Operation(summary = "删除定时任务")
    @DeleteMapping("/del")
    public R<?> deleteJob(@Parameter(description = "作业key名字") @RequestParam String name,
                          @Parameter(description = "作业key组") @RequestParam(required = false) String group){
        quartzJobService.delete(name, group);
        return R.ok();
    }

    @Operation(summary = "暂停定时任务")
    @GetMapping("/pause")
    public R<?> pauseJob(@Parameter(description = "作业key名字") @RequestParam String name,
                         @Parameter(description = "作业key组") @RequestParam(required = false) String group){
        quartzJobService.pauseJob(name, group);
        return R.ok();
    }

    @Operation(summary = "恢复定时任务")
    @GetMapping("/resume")
    public R<?> resumeJob(@Parameter(description = "作业key名字") @RequestParam String name,
                          @Parameter(description = "作业key组") @RequestParam(required = false) String group){
        quartzJobService.resumeJob(name, group);
        return R.ok();
    }
}
